//
//  OverlapLayout.m
//  CollectionTest
//
//  Created by lanou3g on 16/3/12.
//  Copyright © 2016年 郭丹. All rights reserved.
//
#define kItemSizeX 355
#define kItemSizeY 420
#import "OverlapLayout.h"
#import "OverlapCollectionViewCell.h"

@implementation OverlapLayout

- (instancetype)init
{
    self = [super init];
    if (self) {
        //暂时为空
    }
    return self;
}

- (void)prepareLayout {
    [super prepareLayout];
    
//    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

}

- (CGSize)collectionViewContentSize {
    CGSize size = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height - 50);
    return size;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    self.cellCount = [[self collectionView] numberOfItemsInSection:0];
//    NSLog(@"--------%ld",self.cellCount);
    NSMutableArray *attributes = [NSMutableArray array];
    for (NSInteger i = 0; i < self.cellCount; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [attributes addObject:attrs];
    }
    return attributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.hidden = NO;
    if (indexPath.item == 0) {
        attributes.size = CGSizeMake(kItemSizeX, kItemSizeY);
         attributes.center = CGPointMake(self.collectionView.center.x, self.collectionView.center.y - 20);
    } else if (indexPath.item == 1) {
        attributes.size = CGSizeMake(kItemSizeX - 20, kItemSizeY - 10);
        attributes.center = self.collectionView.center;
    } else if(indexPath.item == 2) {
        attributes.size = CGSizeMake(kItemSizeX - 40, kItemSizeY - 20);
        attributes.center = CGPointMake(self.collectionView.center.x, self.collectionView.center.y + 20);
        
    } else {
        attributes.hidden = YES;
        attributes.size = CGSizeMake(kItemSizeX - 60, kItemSizeY - 30);
        attributes.center = CGPointMake(self.collectionView.center.x, self.collectionView.center.y + 20);
    }
    //        zIndex越大，越在上面
    attributes.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item;
    return attributes;
    
}
//显示的边界改变时就重新布局，调用-rect方法，
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}












@end
